var fs = require('fs');

function render_view(request, response, view_id) {
  fs.readFile(view_id, 'utf-8', function(err, data) {
    if(err) {
      var error_message = 'Ocorreu um erro ao tentar carregar o recurso ' + view_id + '.<br>Detalhe: ' + err.toString()
      render_error_page(request, response, error_message);
      console.error('ERROR: ' + error_message);
      
      return;
    }

    render_response(request, response, data);
  });
}

function render_content(request, response, content) {
  render_response(request, response, content);
}

function render_response(request, response, content) {
  response.writeHead(200, {"Content-Type": "text/html;charset=UTF-8"});
  response.write(content);
  response.end();
}

function render_error_page(request, response, content) {
  response.writeHead(500, {"Content-Type": "text/html;charset=UTF-8"});
  response.write(content);
  response.end();
}

exports.render_view = render_view;
exports.render_content = render_content;
